/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-14     ZeroFree     first implementation
 * 2019-07-28     Ernest       perfect player, add record funciton and other APIs
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <stdlib.h>

#include "drv_touch.h"

rt_err_t drv_touch_write_reg(struct rt_i2c_bus_device *dev, rt_uint8_t *data, rt_uint16_t len)
{
    struct rt_i2c_msg msg;
    RT_ASSERT(dev != RT_NULL);

    msg.addr = 0x5A;
    msg.flags = RT_I2C_WR;
    msg.len = len;
    msg.buf = data;
    return rt_i2c_transfer(dev, &msg, 1);
}

rt_err_t drv_touch_read_reg(struct rt_i2c_bus_device *dev, rt_uint8_t *data, rt_uint16_t len)
{
    struct rt_i2c_msg msg;
    RT_ASSERT(dev != RT_NULL);

    msg.addr = 0x5A;
    msg.flags = RT_I2C_RD;
    msg.len = len;
    msg.buf = data;
    return rt_i2c_transfer(dev, &msg, 1);
}


rt_err_t drv_touch_write_read(struct rt_i2c_bus_device *dev, rt_uint8_t *tx, rt_uint16_t tx_len, rt_uint8_t *rx, rt_uint16_t rx_len)
{
    struct rt_i2c_msg msg[2];
    RT_ASSERT(dev != RT_NULL);

    msg[0].addr = 0x5A;
    msg[0].flags = RT_I2C_WR;
    msg[0].len = tx_len;
    msg[0].buf = tx;

    msg[1].addr = 0x5A;
    msg[1].flags = RT_I2C_RD;
    msg[1].len = rx_len;
    msg[1].buf = rx;

    return rt_i2c_transfer(dev, msg, 2);
}

void drv_touch_init(void)
{
    rt_pin_mode(TOUCH_RESET_PIN, PIN_MODE_OUTPUT);
    rt_pin_write(TOUCH_RESET_PIN, RT_TRUE);
    rt_thread_mdelay(10);
    rt_pin_write(TOUCH_RESET_PIN, RT_FALSE);
    rt_thread_mdelay(10);
    rt_pin_write(TOUCH_RESET_PIN, RT_TRUE);
    rt_thread_mdelay(200);
}
